<template lang="pug">
el-dialog(title="创建入库单" :visible.sync="dialog" :before-close="close" append-to-body)
    goodslist(@getgoodsid="getgooditem")
    el-row
        el-col(:span="24")
            el-button(type="primary" @click="save") 保存
            el-button(type="primary" @click="open") 选择入库商品
        el-col.mgt(:span="24")
            el-form(:inline="true" :model="forminline" class="demo-form-inline")
                el-form-item(label="采购日期")
                    el-date-picker(v-model="datetime" type="date" placeholder="选择日期(必填)")
                el-form-item(label="采购渠道")
                    // el-cascader(:options="opt" v-model="city" @change="handleChange" placeholder="必填")
                    el-input.w(placeholder="例:南京江宁众彩水果批发市场" v-model="buyAddress")
    el-row
        el-col(:span="24")
            el-table(:data="td" style="width: 100%" height="350" v-loading="loading" element-loading-text="数据加载中...")
                el-table-column(prop="goodsId" label="商品编号")
                el-table-column(prop="name" label="商品名称")
                el-table-column(prop="spec" label="单位")
                el-table-column(label="单价/元(成本价)")
                    template(slot-scope="scope")
                        el-input(type="number" v-model="scope.row.prices" placeholder="必填")
                el-table-column(label="采购量")
                    template(slot-scope="scope")
                        el-input(type="number" min="1" v-model="scope.row.buyNum" placeholder="必填")
                el-table-column(label="总金额/元(成本价)")
                    template(slot-scope="scope")
                        el-input(type="number" v-model="scope.row.allPrice" placeholder="必填")
                el-table-column(label="供应商(档口号)")
                    template(slot-scope="scope")
                        el-input(type="number" v-model="scope.row.supplier" placeholder="必填")
                el-table-column(label="备注")
                    template(slot-scope="scope")
                        el-input(type="text" v-model="scope.row.remark" placeholder="选填")
                el-table-column(label="操作")
                    template(slot-scope="scope")
                        el-button(size="mini" type="danger" @click="del(scope.$index, scope.row)") 删除
</template>
<script>
import moment from 'moment'
import goodslist from 'components/GoodsLists'
export default{
    data(){
        return{
            shiro:this.$store.state.shiro,
            dialog:false,
            loading:false,
            datetime:'',
            td:[],
            opt:[],
            sheng:0,
            shi:0,
            qu:0,
            city:[],
            forminline:{},
            buyAddress:'',
            shopID:-1
        }
    },
    components:{
        goodslist
    },
    computed:{
        getstate(){
            return this.$store.state.createrukuclass2;
        }
    },
    watch:{
        getstate(val){
            var _this=this
            if(!this.$store.state.shiro){
                if(this.$store.state.TOPshopID){
                    this.shopID=this.$store.state.TOPshopID
                }
            }
            this.opt=[]
            // axios.get('/listProvince.do').then(r=>{
            //     if(r.data.code!=200){
            //         return
            //     }
            //     var array = r.data.response.provinces
            //     function clone(o) {
            //         var temp = {}
            //         for (var key in o) {
            //             if (Object.prototype.toString.call(o[key])=='[object Array]') {
            //                 temp['children'] = new Array()
            //                 for (var i of o[key]) {
            //                     temp['children'] .push(clone(i))
            //                 }
            //             } else {
            //                 if (key.indexOf('Id') != -1) {
            //                     if(!temp['value']) temp['value'] = o[key]
            //                 } else if (key.indexOf('Name') != -1) {
            //                     temp['label'] = o[key]
            //                 }
            //             }
            //         }
            //         return temp
            //     }
            //     for (let i of array ) {
            //         _this.opt.push(clone(i))
            //     }
            // })
            this.dialog=val
        }
    },
    mounted(){
        if(!this.$store.state.shiro){
            if(this.$store.state.TOPshopID){
                this.shopID=this.$store.state.TOPshopID
            }
        }
    },
    methods:{
        close(){
            this.buyAddress=''
            this.td=[]
            this.city=[]
            this.datetime=''
            this.opt=[]
            this.$store.state.createrukuclass2=false
        },
        handleChange(v){
            this.sheng=v[0]
            this.shi=v[1]
            this.qu=v[2]
        },
        chekcall(){//遍历所有填写的地方,效验
            // if(this.sheng==0){
            //     this.$message.error('采购省份不能为空')
            //     return false
            // }
            // if(this.shi==0){
            //     this.$message.error('采购城市不能为空')
            //     return false
            // }
            if(!this.buyAddress){
                this.$message.error('采购地址不能为空')
                return false
            }
            if(!this.datetime){
                this.$message.error('采购时间不能为空')
                return false
            }
            for(var i=0;i<this.td.length;i++){
                if(!this.td[i].prices){
                    this.$message.error('单价不能为空')
                    return false
                }
                if(this.td[i].prices<=0){
                    this.$message.error('单价必须大于0')
                    return false
                }
                if(!this.td[i].buyNum){
                    this.$message.error('采购数量不能为空')
                    return false
                }
                if(!this.td[i].allPrice){
                    this.$message.error('总价不能为空')
                    return false
                }
                if(!this.td[i].supplier){
                    this.$message.error('供应商不能为空')
                    return false
                }
                if(this.td[i].buyNum <= 0){
                    this.$message.error('采购数量不能为负数')
                    return false
                }
            }
            return true
        },
        checkacllback(arr){//遍历请求的返回值
            for(var i=0;i<arr.length;i++){
                if(arr[i].data.code!=200){
                    this.$message.error(arr[i].data.message)
                    return false
                }
                return true
            }
        },
        save(){//保存
            var _this=this
            if(this.shopID==-1){
                _this.$message.info('权限有误,不能添加')
                return
            }
            if(!this.chekcall()){
                return
            }
            this.$confirm('创建后无法修改,是否确定?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                var axiosList=new Array()
                for(var i=0;i<_this.td.length;i++){
                    var pt=new URLSearchParams()
                    pt.append('goodId',_this.td[i].goodsId)
                    pt.append('goodName',_this.td[i].name)
                    // pt.append('buyPro',_this.sheng)
                    // pt.append('buyCity',_this.shi)
                    pt.append('buyAddress',_this.buyAddress)
                    pt.append('spec',_this.td[i].spec)
                    pt.append('price',_this.td[i].prices)
                    pt.append('buyNum',_this.td[i].buyNum)
                    pt.append('buyTime',moment(_this.datetime).format("YYYY-MM-DD HH:mm:ss"))
                    pt.append('allPrice',_this.td[i].allPrice)
                    if(_this.td[i].remark){
                        pt.append('remark',_this.td[i].remark)
                    }
                    pt.append('supplier',_this.td[i].supplier)
                    pt.append('shopId',_this.shopID)
                    axiosList.push(axios.get('/addStock.do',{params:pt}))
                }


                axios.all(axiosList).then(r=>{
                    if(_this.checkacllback(r)){
                       _this.$message.success('入库成功')
                        _this.$store.state.createruku=false
                        _this.close()
                    }else{
                        return
                    }
                })


            }).catch(() => {
                return 
            });
        },
        del(i,r){//删除一行
            this.td.splice(i,1)
        },
        open(){//打开选择商品弹窗
            this.$store.commit('ChangeWxGood')
        },
        getgooditem(m){//获取单个商品详情
            var _this=this
            this.loading=true
            for(var i=0;i<m.length;i++){
                var pp=new URLSearchParams()
                pp.append('xcxGoodsId',m[i].goodsId)
                axios.get('/getXcxGoodsById.do',{params:pp}).then(r=>{
                    // console.log('getXcxGoodsById',r)
                    if(r.data.code==200){
                        _this.td.push(r.data.response.xcxGoods)
                    }else{
                        _this.$message.error(r.data.message)
                        _this.loading=false
                    }
                })
            }
            _this.loading=false
        }
    }
} 
</script>
<style>
.w{
    width: 165px;
    margin-left: 10px;
}
.mgt{
    margin-top: 20px;
}
.flr{
    float: right;
}
</style>
